package com.brisk.web.controller.applet;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brisk.common.annotation.Log;
import com.brisk.common.core.controller.BaseController;
import com.brisk.common.core.domain.AjaxResult;
import com.brisk.common.enums.BusinessType;
import com.ruoyi.applet.domain.AppletVideo;
import com.ruoyi.applet.service.IAppletVideoService;
import com.brisk.common.utils.poi.ExcelUtil;
import com.brisk.common.core.page.TableDataInfo;

/**
 * 视频Controller
 *
 * @author PFD
 * @date 2022-08-18
 */
@RestController
@RequestMapping("/video/video")
public class AppletVideoController extends BaseController {
    @Autowired
    private IAppletVideoService appletVideoService;

    /**
     * 查询视频列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppletVideo appletVideo) {
        startPage();
        List<AppletVideo> list = appletVideoService.selectAppletVideoList(appletVideo);
        return getDataTable(list);
    }

    /**
     * 导出视频列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:export')")
    @Log(title = "视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppletVideo appletVideo) {
        List<AppletVideo> list = appletVideoService.selectAppletVideoList(appletVideo);
        ExcelUtil<AppletVideo> util = new ExcelUtil<AppletVideo>(AppletVideo.class);
        util.exportExcel(response, list, "视频数据");
    }

    /**
     * 获取视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:video:query')")
    @GetMapping(value = "/{videoId}")
    public AjaxResult getInfo(@PathVariable("videoId") Long videoId) {
        return AjaxResult.success(appletVideoService.selectAppletVideoByVideoId(videoId));
    }

    /**
     * 新增视频
     */
    @PreAuthorize("@ss.hasPermi('video:video:add')")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppletVideo appletVideo) {
        int res = appletVideoService.insertAppletVideo(appletVideo);
        if (res < 0) {
            return AjaxResult.error("该标点已有视频，不可重复添加");
        }
        return toAjax(res);
    }

    /**
     * 修改视频
     */
    @PreAuthorize("@ss.hasPermi('video:video:edit')")
    @Log(title = "视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletVideo appletVideo) {
        int res = appletVideoService.updateAppletVideo(appletVideo);
        if (res < 0) {
            return AjaxResult.error("修改后的标点不能存在两个视频");
        }
        return toAjax(res);
    }

    /**
     * 删除视频
     */
    @PreAuthorize("@ss.hasPermi('video:video:remove')")
    @Log(title = "视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{videoIds}")
    public AjaxResult remove(@PathVariable Long[] videoIds) {
        return toAjax(appletVideoService.deleteAppletVideoByVideoIds(videoIds));
    }
}
